<?php

require_once("common.inc.php");

function get_wordgroup_members($wordgroup_id) {

	$member_syntranses = array();

	$query = mysql_query(
		"SELECT *
		FROM wvt_wordgroup_syntranses ws, uw_syntrans s
		WHERE wordgroup_id = $wordgroup_id
		AND s.syntrans_sid = ws.syntrans_sid
		AND s.remove_transaction_id IS NULL"
	)	or die (mysql_error());
	
	while ($row = mysql_fetch_array($query)) {
		$member_syntranses[] = $row['syntrans_sid'];
	}
	
	return $member_syntranses;
	
}

function get_expression_for_dm($dm_id) {

	$search_languages = array_unique(array_merge(array($_SESSION['interface_language']), $_SESSION['language_preference'], getMajorLanguages()));

	foreach($search_languages as $language_id) {
		if($language_id == $_SESSION['learn_language']) continue; //we don't want to give the answer away
		$query = mysql_query(
			"SELECT spelling, s.syntrans_sid, language_id
			FROM
			uw_expression e,
			uw_syntrans s,
			uw_defined_meaning dm,
			wvt_wordgroup_subscriptions wsub,
			wvt_wordgroup_syntranses ws
			WHERE
			e.expression_id = s.expression_id
			AND dm.defined_meaning_id = s.defined_meaning_id
			AND dm.defined_meaning_id = $dm_id
			AND identical_meaning = 1
			AND e.language_id = $language_id
			AND s.syntrans_sid = ws.syntrans_sid
			AND ws.wordgroup_id = wsub.wordgroup_id
			AND wsub.user_id = {$_SESSION['user_id']}
			AND wsub.enabled = 1
			AND e.remove_transaction_id IS NULL
			AND s.remove_transaction_id IS NULL
			AND dm.remove_transaction_id IS NULL
			ORDER BY RAND() /* in case of more than one alternative */
			"
		) or die ("can't get expression in language of preference for DM $dm_id: " . mysql_error());

		//if we find an expression in a language of preference, break from the loop by returning the text
		if (mysql_num_rows($query) > 0) {
			$row = mysql_fetch_array($query);
			return array(
				'spelling' => $row['spelling'],
				'syntrans_sid' => $row['syntrans_sid'],
				'language_id' => $row['language_id']
			);
		}

	}
	
	die("can't find any expression for dm $dm_id in languages of preference and major languages!");
	//TODO: find any expression - alternatively, expand getMajorLanguage() so that it includes all languages
	
}

function get_text_for_dm($dm_id) {
	
	$search_languages = array_unique(array_merge(array($_SESSION['interface_language']), $_SESSION['language_preference'], getMajorLanguages()));
	
	foreach($search_languages as $language_id) {
		
		$query = mysql_query(
			"SELECT text_text
			FROM uw_defined_meaning dm, uw_translated_content tc, uw_text t
			WHERE
			dm.defined_meaning_id = $dm_id
			AND tc.language_id = $language_id
			AND tc.translated_content_id = dm.meaning_text_tcid
			AND t.text_id = tc.text_id
			AND tc.remove_transaction_id IS NULL
			AND dm.remove_transaction_id IS NULL
			"
		) or die ("can't get text for DM $dm_id: " . mysql_error());
		
		//if we find translated content in a language of preference, break from the loop by returning the text
		
		if(mysql_num_rows($query) > 0) {
			$row = mysql_fetch_array($query);
			return $row['text_text'];
		}
	}
	
	die("can't find any text for dm $dm_id in languages of preference and major languages!");
	//TODO: find any text - alternatively, expand getMajorLanguage() so that it includes all languages
	
}


/* currently unused
function get_dm_for_syntrans($syntrans_sid) {

	$query = mysql_query(
		"SELECT s.defined_meaning_id
		FROM uw_syntrans s, uw_defined_meaning dm
		WHERE syntrans_sid = $syntrans_sid
		AND s.defined_meaning_id = dm.defined_meaning_id
		AND dm.remove_transaction_id IS NULL
		AND s.remove_transaction_id IS NULL"
	)	or die (mysql_error());

	$numfound = mysql_num_rows($query);

	if ($numfound > 1) {
		//this is paranoid, but paranoia is good
		die ("expected to find exactly one DM for syntrans $syntrans_sid, found $numfound");
	} else if ($numfound == 0) {
		//TODO: handle (wordgroups with) syntranses of deleted DM's
		die ("no DM found for syntrans $syntrans_sid");
	}

	$row = mysql_fetch_array($query);
	return ($row['defined_meaning_id']);
	
}

*/

function get_shelf_for_syntrans($syntrans_sid, $test_mode = 2) {
	$query = mysql_query(
		"SELECT shelf
		FROM wvt_wordguesses
		WHERE syntrans_sid = $syntrans_sid
		AND test_mode = $test_mode
		AND user_id = {$_SESSION['user_id']}
		"
	) or die ("error fetching shelf for syntrans $syntrans_sid: " . mysql_error());

	$numrows = mysql_num_rows($query);

	if ($numrows > 0) {
		if ($numrows > 1) {
			die ("found more than one row for syntrans_sid $syntrans_sid in wvt_wordguesses for user {$_SESSION['user_id']}, expected one or less");
		} else {
			$row = mysql_fetch_array($query);
			return $row['shelf'];
		}
	} else { //not yet guessed
		return -1;
	}

}

function is_valid_alternative_for_syntrans($answer, $q_id, $src_dm) {

	$answer = mysql_real_escape_string($answer);

	//see if the answer is identical to a syntrans in learn_language for this DM
	$query = mysql_query(
		"SELECT syntrans_sid
		FROM uw_expression e, uw_syntrans s
		WHERE s.defined_meaning_id = $src_dm
		AND s.expression_id = e.expression_id
		AND s.syntrans_sid != $q_id
		AND e.language_id = {$_SESSION['learn_language']}
		AND e.spelling = '$answer'
		AND s.identical_meaning = 1
		AND e.remove_transaction_id IS NULL
		AND s.remove_transaction_id IS NULL
		"
	) or die ("error checking if $answer is a valid answer for $src_dm in language {$_SESSION['learn_language']}");
	
	if (mysql_num_rows($query) > 0) {
		$row = mysql_fetch_array($query);
		$answer_syntrans_sid = $row['syntrans_sid'];
	} else {
		return false;
	}
	
	//see if this syntrans has been guessed yet
	
	$answer_shelf = get_shelf_for_syntrans($answer_syntrans_sid);
	if ($answer_shelf < 0) return $answer_syntrans_sid;
	
	//get the shelf of the expected answer

	$q_shelf = get_shelf_for_syntrans($q_id);

	//see if the shelf of the actual answer is lower or equal to that of the expected one

	if ($answer_shelf <= $q_shelf) {
		return $answer_syntrans_sid;
	} else {
		return false;
	}
}

?>